package com.suray.wcs.web.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suray.basic.wcs.path.core.Lifter;
import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.plc.core.Plc;
import com.suray.basic.wcs.plc.entity.PlcDeviceDB;
import com.suray.basic.wcs.plc.enums.*;
import com.suray.basic.wcs.plc.service.impl.PlcDeviceDBServiceImpl;
import com.suray.basic.wcs.plc.pojo.DeviceBase;
import com.suray.basic.wcs.plc.pojo.oper.lifter.LifterOper;
import com.suray.basic.wcs.plc.pojo.oper.lifter.TwoDirectionRgvLifterOper;
import com.suray.basic.wcs.plc.pojo.oper.lifter.ForkLifterOper;
import com.suray.basic.wcs.plc.pojo.oper.single.AutoHoistOper;
import com.suray.basic.wcs.plc.utils.PlcUnit;
import com.suray.basic.wcs.utils.LockNode;
import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.commin.enmus.BusinessType;
import com.suray.basic.wcs.plc.utils.LifterLayerType;
import com.suray.basic.wcs.plc.utils.LifterLocationType;
import com.suray.basic.wcs.plc.utils.LifterTaskProcess;
import com.suray.basic.wcs.plc.utils.PalletStatus;
import com.suray.wcs.service.core.util.LockNodeManager;
import com.suray.wcs.service.plc.LifterEvent;
import com.suray.wcs.service.plc.LifterTaskManage;
import com.suray.wcs.service.plc.LifterTaskThread;
import com.suray.wcs.service.plc.util.impl.ForkLifterEvent;
import com.suray.wcs.service.pojo.*;
import com.suray.wcs.service.system.service.HoisterOperService;
import com.suray.wcs.service.system.service.impl.NodeDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hoisterOperController")
public class HoisterOperController {
    @Autowired
    private PlcDeviceDBServiceImpl plcdevicedbServiceImpl;
    @Autowired
    private NodeDBServiceImpl nodeDBService;

    @Autowired
    private HoisterOperService hoisterOperService;

    private final ForkLifterEvent forkLifterEvent = SpringContextHolder.getBean(ForkLifterEvent.class);

    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * @param plcDeviceId
     * @return LifterOper
     * @Description:通过plcDeviceId获取设备操作对象
     */
    public LifterOper getDeviceOper(Integer plcDeviceId) {
        return (LifterOper) Plc.deviceLifterCoordMap.get(Node.parseGrid(plcdevicedbServiceImpl.getById(plcDeviceId).getDeviceLocation()));
    }

    /**
     * 获取所有提升机模式及位置信息
     */
    @PostMapping(value = "/lifterInit")
    @ResponseBody
    @ApiOperation(value = "获取所有提升机模式及位置信息")
    public ResponseData lifterInit() {
        List<BaseEum> lifterMode = new ArrayList<>();
        lifterMode.add(new BaseEum(LifterMode.LOAD_PALLET.getModeNum(), "托盘出入"));
        lifterMode.add(new BaseEum(LifterMode.LOAD_RGV.getModeNum(), "载车"));
        lifterMode.add(new BaseEum(LifterMode.UNLOADED.getModeNum(), "空载"));
        lifterMode.add(new BaseEum(LifterMode.DEBUG.getModeNum(), "调试"));
        lifterMode.add(new BaseEum(LifterMode.RGV.getModeNum(), "四向车模式"));
        lifterMode.add(new BaseEum(LifterMode.TWO_DIRECTION_RGV.getModeNum(), "两向车模式"));
        List<BaseEum> lifterPosition = new ArrayList<BaseEum>();
        lifterPosition.add(new BaseEum(Location.INSIDE.getCode(), "提升机内部"));
        lifterPosition.add(new BaseEum(Location.LEFT.getCode(), "提升机左侧"));
        lifterPosition.add(new BaseEum(Location.RIGHT.getCode(), "提升机右侧"));
        Map<String, List<BaseEum>> lifterInfo = new HashMap<String, List<BaseEum>>();
        lifterInfo.put("mode", lifterMode);
        lifterInfo.put("position", lifterPosition);
        return ResponseData.success(lifterInfo);
    }

    /**
     * @param plcDeviceId
     * @return
     * @Description:获取设备信息
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:query')")
    @GetMapping(value = "/getPlcDevice")
    @ResponseBody
    @ApiOperation(value = "获取设备信息")
    public ResponseData getPlcDevice(@RequestParam Integer plcDeviceId) {
        if (plcDeviceId == null) {
            return ResponseData.error("参数不能为空");
        }
        return ResponseData.success("查询成功",
                plcdevicedbServiceImpl.getById(plcDeviceId));
    }

    /**
     * @param plcDeviceId
     * @param lockedBy
     * @return
     * @Description:清除提升机占用
     */
    @GetMapping(value = "/clearLocker")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('hoisterOper:edit')")
    @Log(title = "提升机管理", method = "清除占用", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "清除占用")
    public ResponseData clearLocker(@RequestParam Integer plcDeviceId, @RequestParam Object lockedBy) {
        LifterOper lifterOper = getDeviceOper(plcDeviceId);
        Lifter lifter = nodeDBService.getLifter(Node.parseGrid((plcdevicedbServiceImpl.getById(plcDeviceId)).getDeviceLocation()));
        if (LockNodeManager.checkLifterLocked(lifter, lockedBy)) {
            LockNodeManager.unlockLifter(lifter, lockedBy);
            return ResponseData.success("清除占用成功", lifterOper);
        } else {
            return ResponseData.error("该提升机未被占用，无需清除",
                    plcdevicedbServiceImpl.getById(plcDeviceId));
        }

    }

    /**
     * @param plcDeviceId
     * @return
     * @Description:小车驶出
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:setRgvOut')")
    @Log(title = "提升机管理", method = "小车驶出信号", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/setRgvOut")
    @ResponseBody
    @ApiOperation(value = "小车驶出信号")
    public ResponseData setRgvOut(@RequestParam Integer plcDeviceId) throws IOException {
        if (plcDeviceId == null) {
            return ResponseData.error("提升机编号为空");
        }
        log.info("接收到 设置 " + plcDeviceId + " 号提升机的小车驶出信号");
        hoisterOperService.hoistShuttleOuted(this.getDeviceOper(plcDeviceId));
        return ResponseData.success("设置小车驶出信号发送成功", null);
    }

    /**
     * @param plcDeviceId
     * @return
     * @Description:小车到位
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:setRgvIn')")
    @Log(title = "提升机管理", method = "小车到位信号", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/setRgvIn")
    @ResponseBody
    @ApiOperation(value = "小车到位信号")
    public ResponseData setRgvIn(@RequestParam Integer plcDeviceId) throws IOException {
        if (plcDeviceId == null) {
            return ResponseData.error("提升机编号为空");
        }
        log.info("接收到 设置 " + plcDeviceId + " 号提升机的小车到位信号");
        hoisterOperService.setHoistShuttleArrived(this.getDeviceOper(plcDeviceId));
        return ResponseData.success("设置小车到位信号发送成功");
    }

    /**
     * @param plcDeviceId
     * @return
     * @Description:提升机初始化
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:resetLifter')")
    @Log(title = "提升机管理", method = "提升机初始化", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/resetLifter")
    @ResponseBody
    @ApiOperation(value = "提升机初始化")
    public ResponseData resetLifter(@RequestParam Integer plcDeviceId) throws IOException {
        if (plcDeviceId == null) {
            return ResponseData.error("提升机编号为空");
        }
        log.info("接收到 初始化 " + plcDeviceId + " 号提升机信号");

        LifterOper lifterOper = getDeviceOper(plcDeviceId);
        if (lifterOper == null) {
            return ResponseData.error("设备号为" + plcDeviceId + " 的设备不存在");
        }
        if (!(lifterOper instanceof ForkLifterOper)) {
            // 此处清除提升机故障
            lifterOper.setHoistCurrentErrorClear();
            // 清除完成任务，之后电控会自动给清除当前任务编号
            lifterOper.setHoistFinalTaskNoClear();
            LifterEvent.liftWord.remove(lifterOper.getLockLocation().toString());
            // 当前任务号就是密码
            lifterOper.unlockHoist(lifterOper.getHoistCurrentTaskNo());
        }
        // 清除提升机占用
        for (LockNode lockNode : LockNodeManager.getLockedNodes()) {
            if (lockNode.getX() == lifterOper.getCoord().getX() && lockNode.getY() == lifterOper.getCoord().getY()) {
                log.info("界面解锁提升机" + "提升机：" + nodeDBService.getLifter(new Node(lifterOper.getCoord()))
                        + "占用对象：" + lockNode.getLockedBy());
                LockNodeManager.unlockLifter(nodeDBService.getLifter(new Node(lifterOper.getCoord())), lockNode.getLockedBy());
            }
        }
        if (lifterOper instanceof ForkLifterOper) {
            ForkLifterOper.lifterInterruptMap.put(lifterOper.getSeries(), true);
            lifterOper.clearTask();
            lifterOper.setTaskDes(null);
            lifterOper.setHoistCurrentErrorClear();
            LifterTaskManage lifterTaskManage = LifterTaskManage.getInstance((ForkLifterOper) lifterOper);
            lifterTaskManage.init();
        }
        return ResponseData.success("提升机初始化完成");
    }

    /**
     * 获取单个提升机状态信息
     *
     * @param plcDeviceId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:query')")
    @GetMapping(value = "/getLifterByNo")
    @ResponseBody
    @ApiOperation(value = "获得单个提升机状态信息")
    public ResponseData getLifterByNo(@RequestParam Integer plcDeviceId) {
        PlcDeviceDB plcdevicedb = plcdevicedbServiceImpl.getById(plcDeviceId);
        if (plcdevicedb == null) {
            return ResponseData.error("设备号为 " + plcDeviceId + " 的设备不存在", null);
        }
        return ResponseData.success("提升机信息查询成功", plcdevicedb);
    }

    /**
     * 获取所有提升机状态信息
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:query')")
    @GetMapping(value = "/getAllLifter")
    @ResponseBody
    @ApiOperation(value = "获得所有提升机状态信息")
    public ResponseData getAllLifter() {
        QueryWrapper<PlcDeviceDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PlcDeviceDB::getDeviceType, PlcDeviceType.PANASONIC_LIFT.getTypeNum()).or()
                .eq(PlcDeviceDB::getDeviceType, PlcDeviceType.SIEMENS_HOIST.getTypeNum());
        List<PlcDeviceDB> plcDeviceDBS = plcdevicedbServiceImpl.list(queryWrapper);
        return ResponseData.success("获得所有提升机状态信息成功", plcDeviceDBS);
    }

    /**
     * 提升机移货任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:moveTask')")
    @Log(title = "提升机管理", method = "提升机移货任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机移货任务调度")
    @PostMapping(value = "/moveTask")
    @ResponseBody
    public ResponseData moveTask(@RequestBody MoveTaskRequest request) {
        try {
            log.info("接收到移货任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }
            if (request.getStartLayer() > lifterOper.getGridzMax() || request.getEndLayer() > lifterOper.getGridzMax()) {
                return ResponseData.error("超出提升机最大层范围" + request.getStartLayer() + "" + request.getEndLayer());
            }
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;
            Integer realStartLayer = request.getStartLayer();
            Integer realEndLayer = request.getEndLayer();
            try {
                forkLifterEvent.checkReady(forkLifterOper);
            } catch (RuntimeException e) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备," + e.getMessage());
            }
            int code = LifterMode.LOAD_PALLET.getModeNum() * 1000000 + realStartLayer * 10000
                    + request.getStartLocation() * 1000 + realEndLayer * 10
                    + request.getEndLocation();
            ForkLifterEvent lifterEvent = SpringContextHolder.getBean(ForkLifterEvent.class);
            lifterEvent.callLift(forkLifterOper, code, 9000);
            LifterTaskThread.freshTaskProcess(forkLifterOper, 9000, LifterTaskProcess.INIT);
            new LifterTaskThread(forkLifterOper.getSeries(), 9000, forkLifterOper,
                    realStartLayer, Location.parseLocationCode(request.getStartLocation()),
                    realEndLayer, Location.parseLocationCode(request.getEndLocation())).start();
//            ResponseData responseData = checkLifterTaskEnd(forkLifterOper, 9000, "移货任务");
            return ResponseData.success("移货任务发送成功");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("移货任务故障:" + e.getMessage());
        }
    }

    private ResponseData checkLifterTaskEnd(ForkLifterOper forkLifterOper, int taskId, String des) throws IOException {
        int tick = 0;
        while (true) {
            if (forkLifterOper.getHoistFinalTaskNo() == taskId) {
                forkLifterOper.clearLifterFinishTaskCmdNo();
                forkLifterEvent.setHoistFinalTaskNoClear(forkLifterOper);
                return ResponseData.success(des + "完成!");
            }

            tick++;

            if (tick >= 120) {
                break;
            }

            if (forkLifterOper.isLifterFault()) {
                break;
            }

            WcsUtil.delay(1000);
        }

        return ResponseData.error(des + "失败！");
    }

    /**
     * 提升机行驶任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:runTask')")
    @Log(title = "提升机管理", method = "提升机行驶任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机行驶任务调度")
    @PostMapping(value = "/runTask")
    @ResponseBody
    public ResponseData runTask(@RequestBody RunTaskRequest request) {
        try {
            log.info("接收到行驶任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }

            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            try {
                forkLifterEvent.checkReady(forkLifterOper);
            } catch (RuntimeException e) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备," + e.getMessage());
            }

            LifterTaskManage taskManage = LifterTaskManage.getInstance(forkLifterOper);
            taskManage.initLastLocation();
            forkLifterEvent.gotoTarget(forkLifterOper, 9000, request.getEndLayer(), LifterLayerType.TWO_CAR_LAYER,
                    forkLifterOper.analyzeLocation(request.getEndLocation()), LifterTaskProcess.GO_TO_END);
            taskManage.sendTask(9000);
            taskManage.sendSegment(9000);
            return checkLifterTaskEnd(forkLifterOper, 9000, "前往目标点");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("前往目标点失败！" + e.getMessage());
        }
    }

    /**
     * 提升机换层任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:changeLayer')")
    @Log(title = "提升机管理", method = "提升机换层任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机换层任务调度")
    @PostMapping(value = "/changeLayer")
    @ResponseBody
    public ResponseData changeLayerTask(@RequestBody ChangeLayerRequest request) {
        try {
            log.info("接收到换层任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }

            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            try {
                forkLifterEvent.checkReady(forkLifterOper);
            } catch (RuntimeException e) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备," + e.getMessage());
            }

            forkLifterEvent.changeLayer(forkLifterOper, 9000, request.getEndLayer());
            return checkLifterTaskEnd(forkLifterOper, 9000, "提升机换层");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机换层失败:" + e.getMessage());
        }
    }

    /**
     * 提升机切换任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:changeMode')")
    @Log(title = "提升机管理", method = "提升机切换模式任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机切换模式任务调度")
    @PostMapping(value = "/changeMode")
    @ResponseBody
    public ResponseData changeModeTask(@RequestBody ChangeModeRequest request) {
        try {
            log.info("接收到切换模式任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            if (forkLifterOper.isRuning()) {
                return ResponseData.error(lifterOper.getSeries() + " 当前正在运行中，不能切换模式");
            }

            forkLifterOper.changeMode(request.getPlcMode() == 1);
            if (request.getPlcMode() == 1) {
                return ResponseData.success("调试模式切换成功！");
            } else {
                return ResponseData.success("自动模式切换成功！");
            }
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("模式切换失败:" + e.getMessage());
        }
    }

    /**
     * 提升机修改位移任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:modifyLifter')")
    @Log(title = "提升机管理", method = "提升机修改位移任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机修改位移任务调度")
    @PostMapping(value = "/modifyLifter")
    @ResponseBody
    public ResponseData modifyLifterTask(@RequestBody ModifyLifterRequest request) {
        try {
            log.info("接收到修改提升机位移任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            if (!forkLifterOper.isDebugMode()) {
                return ResponseData.error("请先切换至调试模式");
            }

            forkLifterEvent.setLifterDis(request.getLayer(), request.getCarType() == 2, forkLifterOper);
            return ResponseData.success("接收到修改提升机位移任务成功！");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("接收到修改提升机位移任务失败:" + e.getMessage());
        }
    }

    /**
     * 提升机两向车托盘升降位移修改调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:modifyPallet')")
    @Log(title = "提升机管理", method = "提升机两向车托盘升降位移修改调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机两向车托盘升降位移修改调度")
    @PostMapping(value = "/modifyPallet")
    @ResponseBody
    public ResponseData modifyPalletTask(@RequestBody ModifyPalletRequest request) {
        try {
            log.info("提升机两向车托盘升降位移修改任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            if (!forkLifterOper.isDebugMode()) {
                return ResponseData.error("请先切换至调试模式");
            }

            forkLifterEvent.setCarPalletDis(PalletStatus.parseStatus(request.getPalletType()), forkLifterOper);
            return ResponseData.success("提升机两向车托盘升降位移修改任务成功！");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机两向车托盘升降位移修改任务失败:" + e.getMessage());
        }
    }

    /**
     * 提升机修改两向车位移量任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:modifyCar')")
    @Log(title = "提升机管理", method = "提升机修改两向车位移量任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机修改两向车位移量任务调度")
    @PostMapping(value = "/modifyCar")
    @ResponseBody
    public ResponseData modifyCarTask(@RequestBody ModifyCarRequest request) {
        try {
            log.info("接收到修改提升机两向车位移量任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            if (!forkLifterOper.isDebugMode()) {
                return ResponseData.error("请先切换至调试模式");
            }

            LifterLocationType locationType = forkLifterOper.analyzeLocation(request.getCarLocation());
            forkLifterEvent.setCarDis(request.getLayer(), locationType, forkLifterOper);
            return ResponseData.success("接收到修改提升机两向车位移量任务成功！");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("接收到修改提升机两向车位移量任务失败:" + e.getMessage());
        }
    }

    /**
     * 提升机任务调度
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hoisterOper:lifterAction')")
    @Log(title = "提升机管理", method = "提升机任务调度", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提升机任务调度")
    @PostMapping(value = "/lifterAction")
    @ResponseBody
    public ResponseData lifterAction(@RequestBody LifterActionRequest request) {
        log.info("接收到任务  " + request);
        if (request == null) {
            return ResponseData.error("任务不能为空");
        }
        LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
        if (lifterOper == null) {
            return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
        }
        if(request.getMode() == null || request.getMode().equals("")){
            return ResponseData.error("提升机任务模式不能为空");
        }
        LifterMode lifterMode = LifterMode.parseMode(request.getMode());
        if (lifterOper instanceof TwoDirectionRgvLifterOper) {
            if (lifterMode == LifterMode.LOAD_PALLET) {
                lifterMode = LifterMode.TWO_DIRECTION_RGV;
            } else if (lifterMode == LifterMode.UNLOADED) {
                lifterMode = LifterMode.RGV;
            }
        }
        int startFloor = 0;
        int endFloor = request.getEndLayer();
        Location startLocation = Location.parseLocationCode(request.getStartLocation());
        Location endLocation = Location.parseLocationCode(request.getEndLocation());

        if (lifterMode.getModeNum() == LifterMode.LOAD_PALLET.getModeNum()) {
            startFloor = request.getStartLayer();
        }

        int taskNo = PlcUnit.getAutoIncrementNum();

        if (lifterOper instanceof ForkLifterOper) {
            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;
            try {
                forkLifterEvent.checkReady(forkLifterOper);
            } catch (RuntimeException e) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备," + e.getMessage());
            }
        }
        if (!lifterOper.isReady()) {
            return ResponseData.error("提升机未就绪");
        }
        if (lifterOper.getHoistCurrentTaskNo() != 0) {
            return ResponseData.error("提升机有任务");
        }
        if (lifterMode == LifterMode.LOAD_RGV && !lifterOper.isHaveShuttle()) {
            return ResponseData.error("载车模式提升机需有车");
        }
        if (lifterOper.getError() != 0) {
            return ResponseData.error("提升机故障");
        }
        if (lifterMode == LifterMode.RGV) {
            startFloor = 0;
            startLocation = Location.INSIDE;
            endLocation = Location.INSIDE;
        }
        if (lifterMode == LifterMode.TWO_DIRECTION_RGV) {
            startFloor = request.getStartLayer();
            startLocation = Location.parseLocationCode(request.getStartLocation());
            endFloor = request.getEndLayer();
            endLocation = Location.parseLocationCode(request.getEndLocation());

        }
        log.info("当前模式" + lifterMode + "提升机就绪?: " + lifterOper.isReady() + "提升机无任务?: "
                + (lifterOper.getHoistCurrentTaskNo() == 0) + "提升机内有车?: " + lifterOper.isHaveShuttle());
        int code = lifterMode.getModeNum() * 1000000 + startFloor * 10000 + startLocation.getCode() * 1000
                + endFloor * 10 + endLocation.getCode();
        try {
            lifterOper.callLift(code, taskNo);
        } catch (IOException e) {
            return ResponseData.error("网络异常调用提升机失败!");
        }
        LifterEvent.liftWord.put(lifterOper.getLockLocation().toString(), code);
        log.info("调用提升机任务字：" + code + "任务编号" + taskNo);
        int count = 0;
        while (lifterOper.getHoistFinalTaskNo() != taskNo && count < 60) {
            try {
                Thread.sleep(1000);
                count++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 提升机调用后，自动清除完成任务编号
        try {
            hoisterOperService.hoistFinalTaskNoClear(this.getDeviceOper(request.getPlcDeviceId()));
        } catch (IOException e) {
            return ResponseData.error("网络异常，自动清除完成任务编号失败!");
        }
        log.info("调用提升机后自动清除完成任务编号");
        return ResponseData.success("调用提升机任务字：" + code + "任务编号" + taskNo + "执行结束");
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:hoistTaskClear')")
    @Log(title = "提升机管理", method = "清除提升机当前任务", businessType = BusinessType.UPDATE)
    @GetMapping("/hoistTaskClear")
    @ResponseBody
    @ApiOperation(value = "清除提升机当前任务")
    public ResponseData hoistTaskClear(@RequestParam Integer plcDeviceId) throws IOException {
        log.info("接收到 设置 " + plcDeviceId + " 号提升机清除提升机当前任务信号");

        hoisterOperService.hoistTaskClear(this.getDeviceOper(plcDeviceId));
        return ResponseData.success("清除提升机当前任务成功", null);
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:hoistCurrentErrorClear')")
    @Log(title = "提升机管理", method = "清除提升机故障警报", businessType = BusinessType.UPDATE)
    @GetMapping("/hoistCurrentErrorClear")
    @ResponseBody
    @ApiOperation("清除提升机故障警报")
    public ResponseData hoistCurrentErrorClear(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 号提升机清除提升机故障警报信号");

        hoisterOperService.hoistCurrentErrorClear(this.getDeviceOper(plcDeviceId));

        return ResponseData.success("清除提升机故障警报成功", null);
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:recoverLifterTask')")
    @Log(title = "提升机管理", method = "提升机任务恢复", businessType = BusinessType.UPDATE)
    @GetMapping("/recoverLifterTask")
    @ResponseBody
    @ApiOperation("提升机任务恢复")
    public ResponseData recoverLifterTask(@RequestParam Integer plcDeviceId) throws IOException {
        LifterOper lifterOper = getDeviceOper(plcDeviceId);
        ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;
        if (lifterOper.getError() != 0) {
            return ResponseData.error("提升机存在故障,请先点击故障报警清除按钮！");
        }
        if (forkLifterOper.getTaskId() == null) {
            return ResponseData.error("不存在任务,不需要恢复！");
        }
        if (forkLifterOper.getTaskProcess() != null) {
            int code = forkLifterOper.getCode();
            int startFloor = code / 10000 % 100;
            int startLocation = code / 1000 % 10;
            int endFloor = code / 10 % 100;
            int endLocation = code % 10;
            forkLifterEvent.callLift(forkLifterOper, code, forkLifterOper.getTaskId(), forkLifterOper.getTaskProcess());
            new LifterTaskThread(forkLifterOper.getSeries(), forkLifterOper.getTaskId(), forkLifterOper,
                    startFloor, Location.parseLocationCode(startLocation), endFloor,
                    Location.parseLocationCode(endLocation)).start();
//            return checkLifterTaskEnd(forkLifterOper, 9000, "清除提升机故障警报");
        }
        return ResponseData.success("清除提升机故障警报成功", null);
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:hoistLock')")
    @Log(title = "提升机管理", method = "提升机上锁", businessType = BusinessType.UPDATE)
    @GetMapping("/hoistLock")
    @ResponseBody
    @ApiOperation("提升机上锁")
    public ResponseData hoistLock(@RequestParam Integer password, @RequestParam Integer plcDeviceId) throws IOException {
        log.info("接收到 设置 " + plcDeviceId + " 号提升机上锁信号");

        if (hoisterOperService.hoistLock(password, this.getDeviceOper(plcDeviceId))) {

            return ResponseData.success("提升机上锁成功", null);
        } else {
            return ResponseData.error("提升机上锁失败");
        }

    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:hoistUnlock')")
    @Log(title = "提升机管理", method = "提升机解锁", businessType = BusinessType.UPDATE)
    @GetMapping("/hoistUnlock")
    @ResponseBody
    @ApiOperation("提升机解锁")
    public ResponseData hoistUnlock(@RequestParam Integer password, @RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 号提升机解锁信号");

        if (hoisterOperService.hoistUnlock(password, this.getDeviceOper(plcDeviceId))) {
            return ResponseData.success("提升机解锁成功", null);
        } else {
            return ResponseData.error("提升机解锁失败");
        }

    }

    @GetMapping("/hoistFianlTaskNoClear")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('hoisterOper:hoistFianlTaskNoClear')")
    @Log(title = "提升机管理", method = "完成任务清除", businessType = BusinessType.UPDATE)
    @ApiOperation("完成任务清除")
    public ResponseData hoistFinalTaskNoClear(@RequestParam Integer plcDeviceId) throws IOException {
        log.info("接收到 设置 " + plcDeviceId + " 号提升机完成任务清除信号");

        LifterOper lifterOper = this.getDeviceOper(plcDeviceId);

        hoisterOperService.hoistFinalTaskNoClear(lifterOper);
        if (lifterOper instanceof ForkLifterOper) {
            ((ForkLifterOper) lifterOper).clearLifterFinishTaskCmdNo();
        }

        return ResponseData.success("提升机完成任务清除成功", null);

    }

    @PostMapping("/writeHoistCargoState")
    @PreAuthorize("@ss.hasPermi('hoisterOper:writeHoistCargoState')")
    @Log(title = "提升机管理", method = "写入货物状态", businessType = BusinessType.UPDATE)
    @ApiOperation("写入货物状态")
    @ResponseBody
    public ResponseData writeHoistCargoState(@RequestBody LifterActionRequest lifterActionRequest) {
        try {
            Integer plcDeviceId = lifterActionRequest.getPlcDeviceId();
            Integer endLayer = lifterActionRequest.getEndLayer();
            DeviceBase deviceBase = getDeviceOper(plcDeviceId);
            TwoDirectionRgvLifterOper twoDirectionRgvLifterOper = null;
            if (deviceBase instanceof TwoDirectionRgvLifterOper) {
                twoDirectionRgvLifterOper = (TwoDirectionRgvLifterOper) deviceBase;
            } else {
                return ResponseData.error("该提升机类型不是自卸式提升机,不支持货物写状态");
            }
            Location location = Location.parseLocationCode(lifterActionRequest.getEndLocation());
            if (location == Location.INSIDE) {
                return ResponseData.error("自卸式提升机不支持,写入提升机内有货");
            }
            CargoState cargoState = CargoState.parseCargoStateCode(lifterActionRequest.getCargoState());
            log.info("写入自卸式提升机:" + plcDeviceId + " " + endLayer + "层" + location + "位置" + "货物状态:" + cargoState);
            PlcUnit.writeTwoDirectionRgvCargoState(twoDirectionRgvLifterOper, location, endLayer, cargoState);
            return ResponseData.success("提升机写入货物转态成功");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机写入货物状态错误");
        }
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:palletUp')")
    @Log(title = "提升机管理", method = "两向车托盘升", businessType = BusinessType.UPDATE)
    @GetMapping("/palletUp")
    @ResponseBody
    @ApiOperation(value = "两向车托盘升")
    public ResponseData palletUp(@RequestParam Integer plcDeviceId) throws IOException {
        try {
            log.info("接收到提升机编号" + plcDeviceId + " 的提升机两向车托盘升");

            LifterOper lifterOper = getDeviceOper(plcDeviceId);

            if (lifterOper == null) {
                return ResponseData.error("设备号为" + plcDeviceId + " 的设备不存在");
            }

            if (lifterOper instanceof ForkLifterOper) {
                forkLifterEvent.palletUpManual((ForkLifterOper) lifterOper, 9000);
            }

            checkLifterTaskEnd((ForkLifterOper) lifterOper, 9000, "托盘升");

            return ResponseData.success("提升机两向车托盘升任务成功", null);
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机两向车托盘升任务失败" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('hoisterOper:palletDown')")
    @Log(title = "提升机管理", method = "两向车托盘降", businessType = BusinessType.UPDATE)
    @GetMapping("/palletDown")
    @ResponseBody
    @ApiOperation(value = "两向车托盘降")
    public ResponseData palletDown(@RequestParam Integer plcDeviceId) throws IOException {
        try {
            log.info("接收到提升机编号" + plcDeviceId + " 的提升机两向车托盘降");

            LifterOper lifterOper = getDeviceOper(plcDeviceId);

            if (lifterOper == null) {
                return ResponseData.error("设备号为" + plcDeviceId + " 的设备不存在");
            }

            if (lifterOper instanceof ForkLifterOper) {
                forkLifterEvent.palletDownManual((ForkLifterOper) lifterOper, 9000);
            }

            checkLifterTaskEnd((ForkLifterOper) lifterOper, 9000, "托盘降");

            return ResponseData.success("提升机两向车托盘降任务成功", null);
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机两向车托盘降任务失败" + e.getMessage());
        }
    }

    /**
     * 提升机任务恢复
     */
    @GetMapping("/lifterTaskRecover")
    @PreAuthorize("@ss.hasPermi('hoisterOper:lifterTaskRecover')")
    @Log(title = "提升机管理", method = "提升机任务恢复", businessType = BusinessType.UPDATE)
    @ApiOperation("提升机任务恢复")
    @ResponseBody
    public ResponseData lifterTaskRecover(Integer plcDeviceId) {
        try {
            LifterOper deviceOper = getDeviceOper(plcDeviceId);
            if (deviceOper instanceof TwoDirectionRgvLifterOper) {
                TwoDirectionRgvLifterOper twoDirectionRgvLifterOper = (TwoDirectionRgvLifterOper) deviceOper;
                twoDirectionRgvLifterOper.lifterTaskRecover();
                return ResponseData.success("任务恢复成功");
            } else {
                return ResponseData.error("该提升机不支持任务一键恢复");
            }
        } catch (IOException e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机网络异常");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("任务恢复失败");
        }
    }

    @PostMapping("/setOriginAction")
    @PreAuthorize("@ss.hasPermi('hoisterOper:setOriginAction')")
    @Log(title = "提升机管理", method = "设置原点", businessType = BusinessType.UPDATE)
    @ApiOperation("设置原点")
    @ResponseBody
    public ResponseData setOrigin(@RequestBody OriginType request) throws IOException {
        try {
            log.info("接收到设置原点任务  " + request);
            if (request == null) {
                return ResponseData.error("任务不能为空");
            }
            LifterOper lifterOper = getDeviceOper(request.getPlcDeviceId());
            if (lifterOper == null) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备不存在");
            }

            if (lifterOper.getPlc().getPlcStatus() == Network.OFF_LINE) {
                return ResponseData.error("设备号为" + request.getPlcDeviceId() + " 的设备离线");
            }

            ForkLifterOper forkLifterOper = (ForkLifterOper) lifterOper;

            if (!forkLifterOper.isDebugMode()) {
                return ResponseData.error("请先切换至调试模式");
            }

            if (request.getLifterLocation() == 1) {
                if (forkLifterOper.hasTwoCarInLifter()) {
                    forkLifterOper.setLifterOriginal();
                } else {
                    return ResponseData.error("两向车不在提升机内，不能设置提升机原点");
                }
            } else if (request.getLifterLocation() == 2) {
                forkLifterOper.setCarOriginal();
            } else if (request.getLifterLocation() == 3) {
                forkLifterOper.setCarPalletOriginal();
            }
            return ResponseData.success("提升机设置原点成功");
        } catch (Exception e) {
            WcsUtil.exceptionLog(e, this.getClass());
            return ResponseData.error("提升机设置原点失败:" + e.getMessage());
        }
    }

    /**
     *
     * @param plcDeviceId
     * @return autoHoistOper
     * @Description: 通过plcDeviceId获取自动升降机
     */
    public AutoHoistOper getAHODevice(Integer plcDeviceId) {
        return (AutoHoistOper) Plc.deviceAutoHoistOperCoordMap.get(Node.parseGrid(plcdevicedbServiceImpl.getById(plcDeviceId).getDeviceLocation()));
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:initAutoElevator')")
    @Log(title = "自动升降机管理", method = "设置自动升降机类型", businessType = BusinessType.UPDATE)
    @GetMapping("/initAutoElevator")
    @ResponseBody
    @ApiOperation("设置自动升降机类型")
    public ResponseData initAutoElevator(@RequestParam Integer plcDeviceId) throws IOException {
        log.info("接收到 设置 " + plcDeviceId + " 自动升降机类型");
        plcdevicedbServiceImpl.setDeviceType( this.getAHODevice(plcDeviceId));
        return ResponseData.success("设置自动升降机类型成功");
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftAutoSpeed')")
    @Log(title = "自动升降机管理", method = "设置升降自动速度", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftAutoSpeed")
    @ResponseBody
    @ApiOperation("设置升降自动速度")
    public ResponseData setLiftAutoSpeed(@RequestParam Integer plcDeviceId, @RequestParam Integer speed) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降自动速度");
        if (speed == null) {
            return ResponseData.error("设置升降自动速度不能为空");
        } else if (speed < 1 || speed > 3000) {
            return ResponseData.error("升降自动速度值超出设置范围，设置范围在[1,3000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftAutoSpeed(speed, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降自动速度成功", speed);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftAutoAcc')")
    @Log(title = "自动升降机管理", method = "设置升降自动加速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftAutoAcc")
    @ResponseBody
    @ApiOperation("设置升降自动加速时间")
    public ResponseData setLiftAutoAcc(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降自动加速时间");
        if (time == null) {
            return ResponseData.error("设置升降自动加速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("升降自动加速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftAutoAcc(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降自动加速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftAutoDec')")
    @Log(title = "自动升降机管理", method = "设置升降自动减速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftAutoDec")
    @ResponseBody
    @ApiOperation("设置升降自动减速时间")
    public ResponseData setLiftAutoDec(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降自动减速时间");
        if (time == null) {
            return ResponseData.error("设置升降自动减速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("升降自动减速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftAutoDec(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降自动减速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftManualSpeed')")
    @Log(title = "自动升降机管理", method = "设置升降手动速度", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftManualSpeed")
    @ResponseBody
    @ApiOperation("设置升降手动速度")
    public ResponseData setLiftManualSpeed(@RequestParam Integer plcDeviceId, @RequestParam Integer speed) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降手动速度");
        if (speed == null) {
            return ResponseData.error("设置升降手动速度不能为空");
        } else if (speed < 1 || speed > 3000) {
            return ResponseData.error("升降手动速度值超出设置范围，设置范围在[1,3000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftManualSpeed(speed, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降手动速度成功", speed);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftManualAcc')")
    @Log(title = "自动升降机管理", method = "设置升降手动加速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftManualAcc")
    @ResponseBody
    @ApiOperation("设置升降手动加速时间")
    public ResponseData setLiftManualAcc(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降手动加速时间");
        if (time == null) {
            return ResponseData.error("设置升降手动加速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("升降手动加速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftManualAcc(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降手动加速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setLiftManualDec')")
    @Log(title = "自动升降机管理", method = "设置升降手动减速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setLiftManualDec")
    @ResponseBody
    @ApiOperation("设置升降手动减速时间")
    public ResponseData setLiftManualDec(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 升降手动减速时间");
        if (time == null) {
            return ResponseData.error("设置升降手动减速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("升降手动减速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistLiftManualDec(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置升降手动减速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorAutoSpeed')")
    @Log(title = "自动升降机管理", method = "输送线自动速度", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorAutoSpeed")
    @ResponseBody
    @ApiOperation("输送线自动速度")
    public ResponseData setConveyorAutoSpeed(@RequestParam Integer plcDeviceId, @RequestParam Integer speed) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线自动速度");
        if (speed == null) {
            return ResponseData.error("设置输送线自动速度不能为空");
        } else if (speed < 1 || speed > 3000) {
            return ResponseData.error("输送线自动速度值超出设置范围，设置范围在[1,3000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorAutoSpeed(speed, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线自动速度成功", speed);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorAutoAcc')")
    @Log(title = "自动升降机管理", method = "输送线自动加速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorAutoAcc")
    @ResponseBody
    @ApiOperation("输送线自动加速时间")
    public ResponseData setConveyorAutoAcc(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线自动加速时间");
        if (time == null) {
            return ResponseData.error("设置输送线自动加速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("输送线自动加速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorAutoAcc(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线自动加速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorAutoDec')")
    @Log(title = "自动升降机管理", method = "输送线自动减速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorAutoDec")
    @ResponseBody
    @ApiOperation("输送线自动减速时间")
    public ResponseData setConveyorAutoDec(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线自动减速时间");
        if (time == null) {
            return ResponseData.error("设置输送线自动减速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("输送线自动减速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorAutoDec(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线自动减速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorManualSpeed')")
    @Log(title = "自动升降机管理", method = "输送线手动速度", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorManualSpeed")
    @ResponseBody
    @ApiOperation("输送线手动速度")
    public ResponseData setConveyorManualSpeed(@RequestParam Integer plcDeviceId, @RequestParam Integer speed) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线手动速度");
        if (speed == null) {
            return ResponseData.error("设置输送线手动速度不能为空");
        } else if (speed < 1 || speed > 3000) {
            return ResponseData.error("输送线手动速度值超出设置范围，设置范围在[1,3000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorManualSpeed(speed, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线手动速度成功", speed);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorManualAcc')")
    @Log(title = "自动升降机管理", method = "输送线手动加速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorManualAcc")
    @ResponseBody
    @ApiOperation("输送线手动加速时间")
    public ResponseData setConveyorManualAcc(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线手动加速时间");
        if (time == null) {
            return ResponseData.error("设置输送线手动加速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("输送线手动加速时间超出设置范围，设置范围在[1,5000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorManualAcc(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线手动加速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:setConveyorManualDec')")
    @Log(title = "自动升降机管理", method = "输送线手动减速时间", businessType = BusinessType.UPDATE)
    @GetMapping("/setConveyorManualDec")
    @ResponseBody
    @ApiOperation("输送线手动减速时间")
    public ResponseData setConveyorManualDec(@RequestParam Integer plcDeviceId, @RequestParam Integer time) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 输送线手动减速时间");
        if (time == null) {
            return ResponseData.error("设置输送线手动减速时间不能为空");
        } else if (time < 1 || time > 5000) {
            return ResponseData.error("输送线手动减速时间超出设置范围，设置范围在[1,3000]");
        }
        plcdevicedbServiceImpl.autoHoistConveyorManualDec(time, this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置输送线手动减速时间成功", time);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformRiseMovement')")
    @Log(title = "自动升降机管理", method = "平台升位移量", businessType = BusinessType.UPDATE)
    @GetMapping("/platformRiseMovement")
    @ResponseBody
    @ApiOperation("平台升位移量")
    public ResponseData platformRiseMovement(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 平台升位移量");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isRiseInPlace()){
            return ResponseData.error("设备未在升到位位置");
        }
        plcdevicedbServiceImpl.autoHoistRiseMovement(this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置平台升位移量成功", null);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformRiseMovement')")
    @Log(title = "自动升降机管理", method = "平台降位移量", businessType = BusinessType.UPDATE)
    @GetMapping("/platformDropMovement")
    @ResponseBody
    @ApiOperation("平台降位移量")
    public ResponseData platformDropMovement(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 平台降位移量");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isDropInPlace()){
            return ResponseData.error("设备未在降到位位置");
        }
        plcdevicedbServiceImpl.autoHoistDropMovement(this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置平台降位移量成功", null);
    }
    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformRise')")
    @Log(title = "自动升降机管理", method = "平台上升", businessType = BusinessType.UPDATE)
    @GetMapping("/platformRise")
    @ResponseBody
    @ApiOperation("平台上升")
    public ResponseData platformRise(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 平台上升");

        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(autoHoistOper.isRiseInPlace()){
            return ResponseData.error("平台已在上升位置！");
        }
        if(autoHoistOper.isLimit1()||autoHoistOper.isLimit3()||autoHoistOper.isRiseLimit()||autoHoistOper.isDropLimit()){
            return ResponseData.error("平台上升失败，输送线1#、3#超限或者平台上下限位超限！");
        }
        if((autoHoistOper.isInPlaceSignal1()&&!autoHoistOper.isInPlaceSignal2())||(!autoHoistOper.isInPlaceSignal1()&&autoHoistOper.isInPlaceSignal2())){
            return ResponseData.error("平台上升失败，输送线到位信号不正确！");
        }
        plcdevicedbServiceImpl.platformRise(this.getAHODevice(plcDeviceId));

        return ResponseData.success("平台上升成功", null);
    }
    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformDrop')")
    @Log(title = "自动升降机管理", method = "平台下降", businessType = BusinessType.UPDATE)
    @GetMapping("/platformDrop")
    @ResponseBody
    @ApiOperation("平台下降")
    public ResponseData platformDrop(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 平台下降");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(autoHoistOper.isDropInPlace()){
            return ResponseData.error("平台已在下降位置！");
        }
        if(autoHoistOper.isLimit1()||autoHoistOper.isLimit3()||autoHoistOper.isRiseLimit()||autoHoistOper.isDropLimit()){
            return ResponseData.error("平台下降失败，输送线1#、3#超限或者平台上下限位超限！");
        }
        if((autoHoistOper.isInPlaceSignal1()&&!autoHoistOper.isInPlaceSignal2())||(!autoHoistOper.isInPlaceSignal1()&&autoHoistOper.isInPlaceSignal2())){
            return ResponseData.error("平台下降失败，输送线到位信号不正确！");
        }
        plcdevicedbServiceImpl.platformDrop(this.getAHODevice(plcDeviceId));

        return ResponseData.success("平台下降成功", null);
    }
    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformForwardIn')")
    @Log(title = "自动升降机管理", method = "正转入库", businessType = BusinessType.UPDATE)
    @GetMapping("/platformForwardIn")
    @ResponseBody
    @ApiOperation("正转入库")
    public ResponseData platformForwardIn(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 正转入库");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isRiseInPlace()&&!autoHoistOper.isDropInPlace()){
            return ResponseData.error("平台未到位");
        }
        if(autoHoistOper.isInPlaceSignal1()||autoHoistOper.isInPlaceSignal2()||autoHoistOper.isLimit1()||autoHoistOper.isLimit3()){
            return ResponseData.error("入库目标位置有货，到位信号1或者2有信号或者限位1或者3有信号");
        }
        plcdevicedbServiceImpl.platformAutoForwardIn(this.getAHODevice(plcDeviceId));

        return ResponseData.success("正转入库成功", null);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformForwardOut')")
    @Log(title = "自动升降机管理", method = "正转出库", businessType = BusinessType.UPDATE)
    @GetMapping("/platformForwardOut")
    @ResponseBody
    @ApiOperation("正转出库")
    public ResponseData platformForwardOut(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 正转出库");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isRiseInPlace()&&!autoHoistOper.isDropInPlace()){
            return ResponseData.error("平台未到位");
        }
        if(!autoHoistOper.isInPlaceSignal1()||!autoHoistOper.isInPlaceSignal2()){
            return ResponseData.error("货物位置不正确，到位信号1或者2未有信号");
        }
        if (autoHoistOper.isLimit1()||autoHoistOper.isLimit3()){
            return ResponseData.error("货物位置不正确，限位信号1或者3未有信号");
        }
        plcdevicedbServiceImpl.platformAutoForwardOut(this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置正转入库成功", null);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformReverseIn')")
    @Log(title = "自动升降机管理", method = "反转出库", businessType = BusinessType.UPDATE)
    @GetMapping("/platformReverseIn")
    @ResponseBody
    @ApiOperation("反转出库")
    public ResponseData platformAutoReverseIn(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 反转入库");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isRiseInPlace()&&!autoHoistOper.isDropInPlace()){
            return ResponseData.error("平台未到位");
        }
        if(autoHoistOper.isInPlaceSignal1()||autoHoistOper.isInPlaceSignal2()||autoHoistOper.isLimit1()||autoHoistOper.isLimit3()){
            return ResponseData.error("入库目标位置有货，到位信号1或者2有信号或者限位1或者3有信号");
        }
        plcdevicedbServiceImpl.platformAutoReverseIn(this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置反转入库成功", null);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:platformReverseOut')")
    @Log(title = "自动升降机管理", method = "反转出库", businessType = BusinessType.UPDATE)
    @GetMapping("/platformReverseOut")
    @ResponseBody
    @ApiOperation("反转出库")
    public ResponseData platformAutoReverseOut(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 反转出库");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        if(!autoHoistOper.isControl()){
            return ResponseData.error("设备未就绪");
        }
        if(!autoHoistOper.isRiseInPlace()&&!autoHoistOper.isDropInPlace()){
            return ResponseData.error("平台未到位");
        }

        if(!autoHoistOper.isInPlaceSignal1()||!autoHoistOper.isInPlaceSignal2()){
            return ResponseData.error("货物位置不正确，到位信号1或者2未有信号");
        }
        if (autoHoistOper.isLimit1()||autoHoistOper.isLimit3()){
            return ResponseData.error("货物位置不正确，限位信号1或者3未有信号");
        }
        plcdevicedbServiceImpl.platformAutoReverseOut(this.getAHODevice(plcDeviceId));

        return ResponseData.success("设置反转出库成功", null);
    }

    @PreAuthorize("@ss.hasPermi('autoHoistOper:clearError')")
    @Log(title = "自动升降机管理", method = "自动升降机故障清除", businessType = BusinessType.UPDATE)
    @GetMapping("/clearError")
    @ResponseBody
    @ApiOperation("自动升降机故障清除")
    public ResponseData clearError(@RequestParam Integer plcDeviceId) throws IOException {

        log.info("接收到 设置 " + plcDeviceId + " 号自动升降机故障清除信号");
        AutoHoistOper autoHoistOper=this.getAHODevice(plcDeviceId);
        plcdevicedbServiceImpl.autoHoistClearError(this.getAHODevice(plcDeviceId));

        return ResponseData.success("自动升降机故障清除成功", null);
    }
}
